<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>module RubyVM::AbstractSyntaxTree - RDoc Documentation</title>


<script src="../js/navigation.js" defer></script>
<script src="../js/search.js" defer></script>
<script src="../js/search_index.js" defer></script>
<script src="../js/searcher.js" defer></script>
<script src="../js/darkfish.js" defer></script>

<script src="../js/jquery-3.2.0.min.js"></script>

<script src="../js/vue.min.js"></script>
<script src="../js/js.cookie.min.js"></script>

<link href="../css/fonts.css" rel="stylesheet">
<link id='rdoccss' href="../css/rdoc.css" rel="stylesheet">
<link href="../css/carbon17.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "../";
  var index_rel_prefix = "../";
  var darkModeCsseHref = "../css/rdoc-dm.css"
  var defaultModeCssHref = "../css/rdoc.css"
  // var cssDarkmode = Cookies.get('darkmode');
  
  if( Cookies.get("darkmode") == "true") {
	$('#rdoccss').attr("href", darkModeCsseHref);
}

//  https://cssdeck.com/blog/simple-jquery-stylesheet-switcher/

document.write('<style type="text/css">body{display:none}</style>');

</script>


</head>
<body id="top" role="document" class="module">
  <!-- this is class.html -->

  <div id='actionbar' >
    <div class='wrapper mdiv'>
      <ul class='grids g0'></ul>
    </div> 
    <!-- VERSION HEADER for 3.3.0-preview2 NOT FOUND -->
  </div> <!-- end action bar -->

  <div class='wrapper hdiv'>

    


    <nav id='vapp' role="navigation">
    <div id="project-navigation">
      <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2><a href="../index.html" rel="home">Home</a></h2>

  <div id="table-of-contents-navigation"  >
    <a href="../table_of_contents.html#pages">Pages</a>
    <a href="../table_of_contents.html#classes">Classes</a>
    <a href="../table_of_contents.html#methods">Methods</a>
  </div>
</div>

      <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

    </div>


    

    <button id='toggleThing' @click="toggleNav()" >Show/hide navigation</button>
    <div :class="isOpen ? 'block' : 'hidden' " id='toggleMe'>
      <div id="class-metadata">
        
        
        
        
        
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
  <h3>Methods</h3>

  <ul class="link-list" role="directory">
    <li ><a href="#method-c-node_id_for_backtrace_location">::node_id_for_backtrace_location</a>
    <li ><a href="#method-c-of">::of</a>
    <li ><a href="#method-c-parse">::parse</a>
    <li ><a href="#method-c-parse_file">::parse_file</a>
  </ul>
</div>

      </div>
     </div>
    </nav>


    <div id='extraz'><div class='adzbox-index'  >
      
     </div>         
    </div>

    <main role="main" aria-labelledby="module-RubyVM::AbstractSyntaxTree">
    <h1 id="module-RubyVM::AbstractSyntaxTree" class="module">
      module RubyVM::AbstractSyntaxTree
    </h1>

    <section class="description">
    
<p><a href="AbstractSyntaxTree.html"><code>AbstractSyntaxTree</code></a> provides methods to parse Ruby code into abstract syntax trees. The nodes in the tree are instances of <a href="AbstractSyntaxTree/Node.html"><code>RubyVM::AbstractSyntaxTree::Node</code></a>.</p>

<p>This module is MRI specific as it exposes implementation details of the MRI abstract syntax tree.</p>

<p>This module is experimental and its API is not stable, therefore it might change without notice. As examples, the order of children nodes is not guaranteed, the number of children nodes might change, there is no way to access children nodes by name, etc.</p>

<p>If you are looking for a stable API or an API working under multiple Ruby implementations, consider using the <em>parser</em> gem or Ripper. If you would like to make <a href="AbstractSyntaxTree.html"><code>RubyVM::AbstractSyntaxTree</code></a> stable, please join the discussion at <a href="https://bugs.ruby-lang.org/issues/14844">bugs.ruby-lang.org/issues/14844</a>.</p>

    </section>

      <section id="5Buntitled-5D" class="documentation-section">





                <section id="public-class-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Class Methods</h3>
                </header>

                  <div id="method-c-node_id_for_backtrace_location" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          RubyVM::AbstractSyntaxTree.node_id_for_backtrace_location(backtrace_location)   &rarr; integer
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the node id for the given backtrace location.</p>

<pre class="ruby"><span class="ruby-keyword">begin</span>
  <span class="ruby-identifier">raise</span>
<span class="ruby-keyword">rescue</span> <span class="ruby-operator">=&gt;</span>  <span class="ruby-identifier">e</span>
  <span class="ruby-identifier">loc</span> = <span class="ruby-identifier">e</span>.<span class="ruby-identifier">backtrace_locations</span>.<span class="ruby-identifier">first</span>
  <span class="ruby-constant">RubyVM</span><span class="ruby-operator">::</span><span class="ruby-constant">AbstractSyntaxTree</span>.<span class="ruby-identifier">node_id_for_backtrace_location</span>(<span class="ruby-identifier">loc</span>)
<span class="ruby-keyword">end</span> <span class="ruby-comment"># =&gt; 0</span>
</pre>

                              <div class="method-source-code" id="node_id_for_backtrace_location-source">
            <pre><span class="ruby-comment"># File ruby_3_3_0_preview2/ast.rb, line 111</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">node_id_for_backtrace_location</span> <span class="ruby-identifier">backtrace_location</span>
  <span class="ruby-constant">Primitive</span>.<span class="ruby-identifier">node_id_for_backtrace_location</span> <span class="ruby-identifier">backtrace_location</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-of" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          RubyVM::AbstractSyntaxTree.of(proc, keep_script_lines: RubyVM.keep_script_lines, error_tolerant: false, keep_tokens: false)   &rarr; RubyVM::AbstractSyntaxTree::Node
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          RubyVM::AbstractSyntaxTree.of(method, keep_script_lines: RubyVM.keep_script_lines, error_tolerant: false, keep_tokens: false) &rarr; RubyVM::AbstractSyntaxTree::Node
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns AST nodes of the given <em>proc</em> or <em>method</em>.</p>

<pre class="ruby"><span class="ruby-constant">RubyVM</span><span class="ruby-operator">::</span><span class="ruby-constant">AbstractSyntaxTree</span>.<span class="ruby-identifier">of</span>(<span class="ruby-identifier">proc</span> {<span class="ruby-value">1</span> <span class="ruby-operator">+</span> <span class="ruby-value">2</span>})
<span class="ruby-comment"># =&gt; #&lt;RubyVM::AbstractSyntaxTree::Node:SCOPE@1:35-1:42&gt;</span>

<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">hello</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;hello, world&quot;</span>
<span class="ruby-keyword">end</span>

<span class="ruby-constant">RubyVM</span><span class="ruby-operator">::</span><span class="ruby-constant">AbstractSyntaxTree</span>.<span class="ruby-identifier">of</span>(<span class="ruby-identifier">method</span>(<span class="ruby-value">:hello</span>))
<span class="ruby-comment"># =&gt; #&lt;RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-3:3&gt;</span>
</pre>

<p>See <a href="AbstractSyntaxTree.html#method-c-parse"><code>::parse</code></a> for explanation of keyword argument meaning and usage.</p>

                              <div class="method-source-code" id="of-source">
            <pre><span class="ruby-comment"># File ruby_3_3_0_preview2/ast.rb, line 96</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">of</span> <span class="ruby-identifier">body</span>, <span class="ruby-value">keep_script_lines:</span> <span class="ruby-constant">RubyVM</span>.<span class="ruby-identifier">keep_script_lines</span>, <span class="ruby-value">error_tolerant:</span> <span class="ruby-keyword">false</span>, <span class="ruby-value">keep_tokens:</span> <span class="ruby-keyword">false</span>
  <span class="ruby-constant">Primitive</span>.<span class="ruby-identifier">ast_s_of</span> <span class="ruby-identifier">body</span>, <span class="ruby-identifier">keep_script_lines</span>, <span class="ruby-identifier">error_tolerant</span>, <span class="ruby-identifier">keep_tokens</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-parse" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          RubyVM::AbstractSyntaxTree.parse(string, keep_script_lines: RubyVM.keep_script_lines, error_tolerant: false, keep_tokens: false) &rarr; RubyVM::AbstractSyntaxTree::Node
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Parses the given <em>string</em> into an abstract syntax tree, returning the root node of that tree.</p>

<pre class="ruby"><span class="ruby-constant">RubyVM</span><span class="ruby-operator">::</span><span class="ruby-constant">AbstractSyntaxTree</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-string">&quot;x = 1 + 2&quot;</span>)
<span class="ruby-comment"># =&gt; #&lt;RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-1:9&gt;</span>
</pre>

<p>If <code>keep_script_lines: true</code> option is provided, the text of the parsed source is associated with nodes and is available via <a href="AbstractSyntaxTree/Node.html#method-i-script_lines"><code>Node#script_lines</code></a>.</p>

<p>If <code>keep_tokens: true</code> option is provided, <a href="AbstractSyntaxTree/Node.html#method-i-tokens"><code>Node#tokens</code></a> are populated.</p>

<p><a href="../SyntaxError.html"><code>SyntaxError</code></a> is raised if the given <em>string</em> is invalid syntax. To overwrite this behavior, <code>error_tolerant: true</code> can be provided. In this case, the parser will produce a tree where expressions with syntax errors would be represented by <a href="AbstractSyntaxTree/Node.html"><code>Node</code></a> with <code>type=:ERROR</code>.</p>

<pre class="ruby"><span class="ruby-identifier">root</span> = <span class="ruby-constant">RubyVM</span><span class="ruby-operator">::</span><span class="ruby-constant">AbstractSyntaxTree</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-string">&quot;x = 1; p(x; y=2&quot;</span>)
<span class="ruby-comment"># &lt;internal:ast&gt;:33:in `parse&#39;: syntax error, unexpected &#39;;&#39;, expecting &#39;)&#39; (SyntaxError)</span>
<span class="ruby-comment"># x = 1; p(x; y=2</span>
<span class="ruby-comment">#           ^</span>

<span class="ruby-identifier">root</span> = <span class="ruby-constant">RubyVM</span><span class="ruby-operator">::</span><span class="ruby-constant">AbstractSyntaxTree</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-string">&quot;x = 1; p(x; y=2&quot;</span>, <span class="ruby-value">error_tolerant:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-comment"># (SCOPE@1:0-1:15</span>
<span class="ruby-comment">#  tbl: [:x, :y]</span>
<span class="ruby-comment">#  args: nil</span>
<span class="ruby-comment">#  body: (BLOCK@1:0-1:15 (LASGN@1:0-1:5 :x (LIT@1:4-1:5 1)) (ERROR@1:7-1:11) (LASGN@1:12-1:15 :y (LIT@1:14-1:15 2))))</span>
<span class="ruby-identifier">root</span>.<span class="ruby-identifier">children</span>.<span class="ruby-identifier">last</span>.<span class="ruby-identifier">children</span>
<span class="ruby-comment"># [(LASGN@1:0-1:5 :x (LIT@1:4-1:5 1)),</span>
<span class="ruby-comment">#  (ERROR@1:7-1:11),</span>
<span class="ruby-comment">#  (LASGN@1:12-1:15 :y (LIT@1:14-1:15 2))]</span>
</pre>

<p>Note that parsing continues even after the errored expression.</p>

                              <div class="method-source-code" id="parse-source">
            <pre><span class="ruby-comment"># File ruby_3_3_0_preview2/ast.rb, line 58</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">parse</span> <span class="ruby-identifier">string</span>, <span class="ruby-value">keep_script_lines:</span> <span class="ruby-constant">RubyVM</span>.<span class="ruby-identifier">keep_script_lines</span>, <span class="ruby-value">error_tolerant:</span> <span class="ruby-keyword">false</span>, <span class="ruby-value">keep_tokens:</span> <span class="ruby-keyword">false</span>
  <span class="ruby-constant">Primitive</span>.<span class="ruby-identifier">ast_s_parse</span> <span class="ruby-identifier">string</span>, <span class="ruby-identifier">keep_script_lines</span>, <span class="ruby-identifier">error_tolerant</span>, <span class="ruby-identifier">keep_tokens</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-parse_file" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          RubyVM::AbstractSyntaxTree.parse_file(pathname, keep_script_lines: RubyVM.keep_script_lines, error_tolerant: false, keep_tokens: false) &rarr; RubyVM::AbstractSyntaxTree::Node
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Reads the file from <em>pathname</em>, then parses it like <a href="AbstractSyntaxTree.html#method-c-parse"><code>::parse</code></a>, returning the root node of the abstract syntax tree.</p>

<p><a href="../SyntaxError.html"><code>SyntaxError</code></a> is raised if <em>pathname</em>’s contents are not valid Ruby syntax.</p>

<pre class="ruby"><span class="ruby-constant">RubyVM</span><span class="ruby-operator">::</span><span class="ruby-constant">AbstractSyntaxTree</span>.<span class="ruby-identifier">parse_file</span>(<span class="ruby-string">&quot;my-app/app.rb&quot;</span>)
<span class="ruby-comment"># =&gt; #&lt;RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-31:3&gt;</span>
</pre>

<p>See <a href="AbstractSyntaxTree.html#method-c-parse"><code>::parse</code></a> for explanation of keyword argument meaning and usage.</p>

                              <div class="method-source-code" id="parse_file-source">
            <pre><span class="ruby-comment"># File ruby_3_3_0_preview2/ast.rb, line 75</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">parse_file</span> <span class="ruby-identifier">pathname</span>, <span class="ruby-value">keep_script_lines:</span> <span class="ruby-constant">RubyVM</span>.<span class="ruby-identifier">keep_script_lines</span>, <span class="ruby-value">error_tolerant:</span> <span class="ruby-keyword">false</span>, <span class="ruby-value">keep_tokens:</span> <span class="ruby-keyword">false</span>
  <span class="ruby-constant">Primitive</span>.<span class="ruby-identifier">ast_s_parse_file</span> <span class="ruby-identifier">pathname</span>, <span class="ruby-identifier">keep_script_lines</span>, <span class="ruby-identifier">error_tolerant</span>, <span class="ruby-identifier">keep_tokens</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                          </section>

              </section>
              </main>



            </div>  <!--  class='wrapper hdiv' -->


<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a></p>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.4.0.</p>
<p>Based on <a href="https://github.com/ged/darkfish/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.</p>

  
    <p><p><a href="https://ruby-doc.org">Ruby-doc.org</a> is a service of <a href="https://jamesbritt.com">James Britt</a> and <a href="https://neurogami.com">Neurogami</a>, purveyors of fine <a href='https://jamesbritt.bandcamp.com/'>dance noise</a></p>
</p>
  
  </footer>

<script type="text/javascript">


  let ads  = $("#carbonads-container").children().detach();


  function swapMode() {
    var cookieName = 'darkmode';
    var cssDarkmode = Cookies.get(cookieName);
    console.log("***** swapMode! " + cssDarkmode + " *****");


    if (cssDarkmode == "true") {
      console.log("We have dark mode, set the css to light ...");
      $('#rdoccss').attr("href", defaultModeCssHref);
      $('#cssSelect').text("Dark mode");
      cssDarkmode = "false";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    } else {
      console.log("We not have dark mode, set the css to dark ...");
      $('#rdoccss').attr("href", darkModeCsseHref);
      $('#cssSelect').text("Light mode");
      cssDarkmode = "true";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    }

    console.log("  --------------- ");
  }


const vueCssApp = new Vue({
el: '#menubar',
data: {
isDark: false
},
methods: {
toggleClass: function(event){
this.isDark = !this.isDark;
}
}
})

const vueApp = new Vue({
el: '#vapp',
data: { 
isOpen: true
},

mounted() {
this.handleResize();
this.manage_mob_classes();
window.addEventListener('resize', this.handleResize)
//this.isOpen !=  (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
created() {
//manage_mob_classes();
},

methods : {
isMobile() {
  return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},

  handleResize() {
    if (!this.isMobile()) {
      this.isOpen = window.innerWidth > 800;
    }
  },

  manage_mob_classes() {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      $("nav").addClass("mob_nav");
      $("main").addClass("mob_main");
      $("#extraz").addClass("mob_extraz");
      $("#carbonads-container").addClass("mob_carbonads-container");
      this.isOpen  = false;
    } else {
      $("nav").removeClass("mob_nav") 
        $("main").removeClass("mob_main");
      $("#extraz").removeClass("mob_extraz");
      $("#carbonads-container").removeClass("mob_carbonads-container");
      this.isOpen  = true;
    }
  },

  toggleNav() {
    this.isOpen =! this.isOpen ;
    // alert("Toggle nav!");
    console.log("toggleNav() click: " + this.isOpen );
  }
}
})

$("#carbonads-container").append(ads);


$(function() {

    var darkmode = Cookies.get("darkmode");
    console.log("Document ready: " + darkmode);

    if ( darkmode  == "true" ) {
      $('#cssSelect').text("Light mode");
    } else {
      $('#cssSelect').text("Dark mode");
     }

    $('body').css('display','block');
    });

</script>

    
  </body> 
</html>

